using System;
using System.Collections.Generic;
using System.Text;
using System.Data.Common;
using System.Data;
using System.IO;
using Runda.CMS.Common.Utility;
using Microsoft.Practices.EnterpriseLibrary.Data;

namespace Runda.CMS.Common.Exchange
{
    public class MailHelper:DalBase
    {
        public enum MailStatus
        {
            NotSend = 0,
            Sucess = 1,
            Failed = 2,
        }


        public static void UpdateMessageStatus(Guid messageID, MailStatus status)
        {
            Database db = DatabaseFactory.CreateDatabase();
            string sqlCommand = "UPDATE  Cms_Sys_Mail SET Status = @Status WHERE MailMsgID=@MailMsgID ";
            DbCommand dbCommand = db.GetSqlStringCommand(sqlCommand);

            db.AddInParameter(dbCommand, "@Status", DbType.Int32);
            db.SetParameterValue(dbCommand, "@Status", (int)status);
            db.AddInParameter(dbCommand, "@MailMsgID", DbType.Guid);
            db.SetParameterValue(dbCommand, "@MailMsgID", messageID);

            db.ExecuteNonQuery(dbCommand);
        }

        public static DataTable GetMailMessages(int msgCount)
        {
            Database db = DatabaseFactory.CreateDatabase();

            string sqlCommand = "SELECT TOP " + msgCount + " * FROM Cms_Sys_Mail WHERE Status = 0 ";

            DbCommand dbCommand = db.GetSqlStringCommand(sqlCommand);

            DataSet result = db.ExecuteDataSet(dbCommand);

            if (result != null && result.Tables.Count > 0) return result.Tables[0];
            else return null;
        }

        public static List<MailAttachment> GetMailAttachments(Guid MailMsgID)
        {
            Database db = DatabaseFactory.CreateDatabase();
            List<MailAttachment> AttchList = new List<MailAttachment>();
            string sql = "select * from Cms_Sys_MailAttachment where MailMsgID=@MailMsgID";
            DbCommand DbCmd = db.GetSqlStringCommand(sql);
            db.AddInParameter(DbCmd, "@MailMsgID", DbType.Guid, MailMsgID);
            using (IDataReader dr = db.ExecuteReader(DbCmd))
            {
                if (dr.Read())
                {
                    MailAttachment atta = new MailAttachment();
                    atta.AttachmentName = dr["AttachmentName"].ToString();

                    byte[] File = null;
                    File = (byte[])dr["AttachmentData"];
                    System.IO.Stream stream = new System.IO.MemoryStream(File);

                    atta.AttachmentData = stream;

                    AttchList.Add(atta);
                }
            }

            return AttchList;
        }


        public static void SendEmail(string to, string toDisplay, string cc, string ccDisplay, string subject, string body, List<MailAttachment> Attatch)
        {
            using (System.Transactions.TransactionScope tran = new System.Transactions.TransactionScope())
            {
                Database db = DatabaseFactory.CreateDatabase();
                Guid MailMsgID = Guid.NewGuid();
                string sqlCommand = @"INSERT INTO [Cms_Sys_Mail]([MailMsgID] ,[Receivers],[CCReceivers],[Subject],[Body],[ReceiverDisplayName],[CCReceiverDisplayName]) VALUES (@MailMsgID ,@Receivers,@CCReceivers,@Subject,@Body,@ReceiverDisplayName,@CCReceiverDisplayName)";

                DbCommand dbCommand = db.GetSqlStringCommand(sqlCommand);
                db.AddInParameter(dbCommand, "@MailMsgID", DbType.Guid, MailMsgID);
                db.AddInParameter(dbCommand, "@Receivers", DbType.String, to);
                db.AddInParameter(dbCommand, "@CCReceivers", DbType.String, cc);
                db.AddInParameter(dbCommand, "@Subject", DbType.String, subject);
                db.AddInParameter(dbCommand, "@Body", DbType.String, body);
                db.AddInParameter(dbCommand, "@ReceiverDisplayName", DbType.String, toDisplay);
                db.AddInParameter(dbCommand, "@CCReceiverDisplayName", DbType.String, ccDisplay);
                db.ExecuteNonQuery(dbCommand);

                if (Attatch != null)
                {
                    foreach (MailAttachment atta in Attatch)
                    {
                        sqlCommand = "insert into Cms_Sys_MailAttachment(MailMsgID,AttachmentData,AttachmentName) values (@MailMsgID,@AttachmentData,@AttachmentName)";

                        dbCommand = db.GetSqlStringCommand(sqlCommand);
                        byte[] bytes = new byte[atta.AttachmentData.Length];
                        atta.AttachmentData.Read(bytes, 0, Convert.ToInt32(atta.AttachmentData.Length));

                        db.AddInParameter(dbCommand, "@MailMsgID", DbType.Guid, MailMsgID);
                        db.AddInParameter(dbCommand, "@AttachmentData", DbType.Binary, bytes);
                        db.AddInParameter(dbCommand, "@AttachmentName", DbType.String, atta.AttachmentName);
                        db.ExecuteNonQuery(dbCommand);
                    }
                }

                tran.Complete();
            }
        }
    }
}
